﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Collections;
using ProjectWithAccess.ServiceReference1;

namespace ProjectWithAccess
{
    public partial class Zeleznica2 : System.Web.UI.UserControl
    {
        ServiceReference1.Monopol27SoapClient client = new Monopol27SoapClient();
        public static int ID_zeleznica = 2;

        // Definiranje na nov element vo Property na korisnickata kontrola, so koj se zadava imeto na zeleznicata
        public string Name
        {
            set
            {
                name.Text = value;
            }
        }


        //Funkcija za polnenje na DropDown Lista so mozni destinacii za patuvanje

        private void fillDropDown(int id_igrac)
        {

            string str = "SELECT ID,Ime FROM TabelaZeleznicka WHERE ID_sopstvenik=" + id_igrac.ToString();

            using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
            {
                using (OleDbCommand cmd = new OleDbCommand(str, con))
                {
                    cmd.CommandType = CommandType.Text;
                    con.Open();
                    OleDbDataReader reader = cmd.ExecuteReader();
                    DropDownList1.DataSource = reader;
                    DropDownList1.DataValueField = "ID";
                    DropDownList1.DataTextField = "Ime";
                    DropDownList1.DataBind();
                    DropDownList1.Items.Insert(0, "Дестинација...");
                    con.Close();
                }
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (client.getSostojba(ID_zeleznica).Equals("sloboden"))
            {
                ImageButton3.Visible = false;
            }
            else
            {
                ImageButton3.Visible = true;
            }
            if (!IsPostBack)
            {

                Panel1.Visible = true;
                Panel2.Visible = false;
                Panel3.Visible = false;

                if (client.getSostojba(ID_zeleznica).Equals("hipoteka"))
                {
                    TextBox1.Enabled = false;
                    Label6.ForeColor = Color.Gray;
                    CheckBox1.Enabled = false;
                    Label7.ForeColor = Color.Gray;
                    Button1.Visible = false;
                }
                //Inicijaliziranje na Labelite od panelot Sopstvenik
                Label11.Text = client.getCena(ID_zeleznica) + " $";
                Label12.Text = client.getSostojba(ID_zeleznica);
                //Inicijalizacija na Check Box-ot vo panelot Sopstvenik, so koj se ovozmozuva patuvanje i se 
                // inicijalizira so vrednost od baza
                CheckBox1.Checked = client.getPatuvanje(ID_zeleznica);

                //Inicijaliziranje na labelite, DropDown listata od panelot Igrac
                //i inicijalizacija na sesiskata promenliva Momentalen_igrac
                Label1.Text = client.getCena(ID_zeleznica) + " $";
                Label2.Text = client.getSostojba(ID_zeleznica);
                Label3.Text = client.getID_sopstvenik(ID_zeleznica).ToString();
                fillDropDown(client.getID_sopstvenik(ID_zeleznica));
            }

        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = false;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;
        }

        // Socuvuvanje na vnesenite podatoci vo baza od panelot na Sopstvenikot 
        protected void Button1_Click(object sender, EventArgs e)
        {
            string cena = TextBox1.Text;

            if (!cena.Equals(""))
            {
                client.setCena(Convert.ToInt32(cena), ID_zeleznica);
            }


            if (CheckBox1.Checked == true)
            {
                client.ovozmoziPatuvanje(true, ID_zeleznica);
            }
            else
            {
                client.ovozmoziPatuvanje(false, ID_zeleznica);
            }


            Label5.Text = "Барањето е извршено!";

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int id_momentalen_igrac = client.getMomentalenIgrac(ID_zeleznica);
            client.kupiZeleznicka(id_momentalen_igrac, ID_zeleznica);
            Label4.Text = "Купувањето е извршено!";
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int destinacija = Convert.ToInt32(DropDownList1.SelectedValue);
            client.setDestinacija(ID_zeleznica, destinacija);
            Label14.Text = "Дестинацијата е изберена и регистрирана во системот!";
        }

    }
}